/*
 * Public libusb header file
 * Copyright (c) 2010 Chen Wang  <cnnbboy@163.com>
 *
 * This software is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <QDebug>
#include "QDnwUsbDeviceManager.h"

#include "libusb/libusb.h"

QDnwUsbDeviceManager::QDnwUsbDeviceManager(QObject *parent) :
    QObject(parent)
{
    this->ctx = NULL;
    this->devHandle = NULL;
    libusb_init( &ctx );
    libusb_set_debug(ctx, 3);
}

QDnwUsbDeviceManager::~QDnwUsbDeviceManager()
{

}

void QDnwUsbDeviceManager::QDnwUsbDeviceManager_SetSmdkType()
{
    libusb_free_device_list(devs, 1);
    libusb_exit( ctx );
}

void QDnwUsbDeviceManager::QDnwUsbDeviceManager_FindSmdkType()
{
    ssize_t cnt;
    cnt = libusb_get_device_list(ctx, &devs);
    qDebug() << "1";
    if(cnt < 0) {
        qDebug() << "Get Device Error";
    }
    qDebug() << cnt;

    for(int i = 0;i < cnt;i++) {
        libusb_device_descriptor desc;
        libusb_device *dev = devs[i];
        qDebug() << "3";
        int ret = libusb_get_device_descriptor(dev, &desc);
        qDebug() << "4";
        if(ret < 0) {
            qDebug() << "Failed to get device descriptor";
            return ;
        }

        qDebug() << "ProductId:" << desc.idProduct << "VendorId" << desc.idVendor;
    }
}
